package org.example.seata.bs.product.controller;

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.example.seata.bs.api.dto.order.OrderInfoDto;
import org.example.seata.bs.api.entity.product.Product;
import org.example.seata.bs.product.service.ProductEnhanceService;
import org.example.seata.common.core.util.ApiResp;
import org.example.seata.common.security.config.resource.Inner;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author hzq
 * @date 2021/11/30 17:33
 */
@RestController
@RequestMapping("/product")
@AllArgsConstructor
public class ProductController {

    private final ProductEnhanceService productEnhanceService;

    /**
     * 不需要认证
     */
    @Inner(false)
    @GetMapping("/{id}")
    @ApiOperation("根据id查询产品信息")
    public ApiResp<Product> getById(@PathVariable Integer id) {
        return ApiResp.ofSuccess(productEnhanceService.getById(id));
    }

    /**
     * 需要认证
     */
    @PutMapping("/{id}")
    @ApiOperation("根据id查询产品信息")
    public ApiResp<Product> putById(@PathVariable Integer id) {
        return ApiResp.ofSuccess(productEnhanceService.getById(id));
    }

    /**
     * 有内部调用标记 || 需要认证
     */
    @Inner
    @PostMapping("/{id}")
    @ApiOperation("根据id查询产品信息")
    public ApiResp<Product> postById(@PathVariable Integer id) {
        return ApiResp.ofSuccess(productEnhanceService.getById(id));
    }

    /**
     * 需要认证
     */
    @PutMapping("/minusProductVirtualInventory")
    @ApiOperation("减去产品虚拟库存")
    public ApiResp<Boolean> minusProductVirtualInventory(@RequestBody @Valid List<OrderInfoDto> orderInfoDtoList) {
        return productEnhanceService.minusProductVirtualInventory(orderInfoDtoList);
    }

    /**
     * 需要认证
     */
    @PutMapping("/plusProductVirtualInventory")
    @ApiOperation("加上产品虚拟库存")
    public ApiResp<Boolean> plusProductVirtualInventory(@RequestBody @Valid List<OrderInfoDto> orderInfoDtoList) {
        return productEnhanceService.plusProductVirtualInventory(orderInfoDtoList);
    }

}
